package com.lenovo.lcdm.dcm.service.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.google.common.collect.Lists;
import com.lenovo.lcdm.common.enums.CommonMsgEnum;
import com.lenovo.lcdm.common.exception.BusinessException;
import com.lenovo.lcdm.dcm.common.util.ContextUtil;
import com.lenovo.lcdm.dcm.dto.ConfigurationKeywordsDto;
import com.lenovo.lcdm.dcm.entity.DcmBusinessConfigurationKeywords;
import com.lenovo.lcdm.dcm.enums.ConfigurationTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther xujing30
 * @Date 2023/11/10
 * @Description
 */
public class DcmBusinessConfigurationKeywordsListener extends AnalysisEventListener<ConfigurationKeywordsDto> {

    private List<DcmBusinessConfigurationKeywords> cachedDataList = Lists.newArrayList();

    private String errorMsg = new String();

    private int eofIndex = -1;

    private static final String line = "Line ";

    public List<DcmBusinessConfigurationKeywords> getAllData() {
        return cachedDataList;
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        throw exception;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        if (context.readSheetHolder().getSheetName().equals(ConfigurationTypeEnum.KEYWORDS.getSheetName())) {
            if (!headMap.values().contains("Category Name")) {
                errorMsg += "Cannot find title:Category Name.\n";
            }
            if (!headMap.values().contains("Product Segment")) {
                errorMsg += "Cannot find title:Product Segment.\n";
            }
            if (!headMap.values().contains("Business Segment")) {
                errorMsg += "Cannot find title:Business Segment.\n";
            }
            if (!headMap.values().contains("Match Type")) {
                errorMsg += "Cannot find title:Match Type.\n";
            }
            if (!headMap.values().contains("Original Value")) {
                errorMsg += "Cannot find title:Original Value.\n";
            }
            if (!headMap.values().contains("GV Replacement Value")) {
                errorMsg += "Cannot find title:GV Replacement Value.\n";
            }
            if (!headMap.values().contains("Reference CV Replacement Value")) {
                errorMsg += "Cannot find title:Reference CV Replacement Value.\n";
            }
        }
    }


    @Override
    public void invoke(ConfigurationKeywordsDto data, AnalysisContext context) {
        if (context.readSheetHolder().getSheetName().equals(ConfigurationTypeEnum.KEYWORDS.getSheetName()) && eofIndex == -1) {
            if (StringUtils.isEmpty(data.getCategory())) {
                errorMsg += line + (context.readRowHolder().getRowIndex() + 1) + ", Category Name cannot be empty.\n";
            } else if (data.getCategory().equals("#EOF")) {
                eofIndex = cachedDataList.size();
                return;
            }
            if (StringUtils.isEmpty(data.getMatchType())) {
                errorMsg += line + (context.readRowHolder().getRowIndex() + 1) + ", Match Type cannot be empty.\n";
            }
            if (StringUtils.isEmpty(data.getOriginalValue())) {
                errorMsg += line + (context.readRowHolder().getRowIndex() + 1) + ", Original Value cannot be empty.\n";
            }
            if (errorMsg.length() == 0) {
                DcmBusinessConfigurationKeywords keyword = new DcmBusinessConfigurationKeywords();
                BeanUtils.copyProperties(data, keyword);
                keyword.setCreator(ContextUtil.getCurrentUser());
                keyword.setCreateTime(new Date());
                keyword.setModifier(ContextUtil.getCurrentUser());
                keyword.setModifyTime(new Date());
                cachedDataList.add(keyword);
            }
        }
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (errorMsg.length() > 0) {
            throw new BusinessException(errorMsg, CommonMsgEnum.FAIL.getCode());
        }
    }

    @Override
    public boolean hasNext(AnalysisContext context) {
        return true;
    }
}
